package com.yunxi.controller.activiti;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yunxi.activiti.pojo.ActivitiManageVO;
import com.yunxi.activiti.service.ActivitiManageDubboService;
import com.yunxi.activiti.service.ActivitiModelDubboService;
import com.yunxi.common.config.OSSUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

@RestController
@RequestMapping("/admin/activiti")
@Api(tags = "activiti:manage", description = "工作流接口")
public class ActivitiManageController {
    private static final Logger logger = LoggerFactory.getLogger(ActivitiManageController.class);

/*    @Reference(version = "${demo.service.version}",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}",
            url = "${dubbo.application.url}",
            check = false,
            timeout = 10000
    )*/
   @Autowired
    private ActivitiModelDubboService activitiModelDubboService;

   @Autowired
   private ActivitiManageDubboService activitiManageDubboService;

    /**
     * 去画流程图页面
     * @param request
     * @param response
     */
    @PostMapping("/createModel")
    @ApiOperation(value = "创建流程图模型")
    public Object createModel(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        Map model = activitiModelDubboService.createModel();
        return ResponseUtil.ok(model);
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    public ApiResult page(ActivitiManageVO activitiManageVO, Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(activitiManageDubboService.findPageByVO(activitiManageVO,pageNum,pageSize));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询列表")
    public ApiResult list(ActivitiManageVO activitiManageVO) throws Exception {
        return ResponseUtil.ok(activitiManageDubboService.listByVo(activitiManageVO));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    public ApiResult load(ActivitiManageVO activitiManageVO) throws Exception {
        return ResponseUtil.ok(activitiManageDubboService.getById(activitiManageVO.getId()));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody ActivitiManageVO activitiManageVO) throws Exception {
        activitiManageDubboService.saveOrUpdate(activitiManageVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<ActivitiManageVO> activitiManageVOList) throws Exception {
        activitiManageDubboService.removeByListVo(activitiManageVOList);
        return ResponseUtil.ok("success");
    }

    /**
     * 通过modelId部署流程
     */
    @PostMapping(value = "/deployModel")
    @ApiOperation(value = "部署流程")
    public ApiResult deployModel(@RequestBody ActivitiManageVO  am) throws Exception {
        logger.debug("通过modelId部署流程");

        return  activitiManageDubboService.deployModel(am);
    }

    @GetMapping("/listInstanceNodeByBusinessKey")
    @ApiOperation(value = "根据业务id读取流程列表")
    public ApiResult listInstanceNodeByBusinessKey(ActivitiManageVO activitiManageVO) throws Exception {
        return ResponseUtil.ok(activitiManageDubboService.listInstanceNodeByBusinessKey(activitiManageVO.getBusinessKey()));
    }

    @GetMapping("/listInstanceNodeByInstanceId")
    @ApiOperation(value = "根据流程实例id读取列表")
    public ApiResult listInstanceNodeByInstanceId(ActivitiManageVO activitiManageVO) throws Exception {
        return ResponseUtil.ok(activitiManageDubboService.listInstanceNodeByInstanceId(activitiManageVO.getInstanceId()));
    }
}
